package com.food.user.controller;


import com.food.user.commos.Result;
import com.food.user.dto.OrderDetailDto;
import com.food.user.dto.OrderListDto;
import com.food.user.exception.MyParameterException;
import com.food.user.form.CreateOrderForm;
import com.food.user.service.OrderService;
import com.food.user.util.WebUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 外卖订单 前端控制器
 * </p>
 *
 * @author 帅得一
 * @since 2023年06月17日
 */
@RestController
@RequestMapping("/order")
@Api(tags = "用户端订单管理接口")
public class OrderController {
    @Autowired
    private OrderService orderService;


    @PostMapping("/createOrder")
    @ApiOperation("下单")
    public Result createOrder(@RequestBody CreateOrderForm form, BindingResult br)throws Exception{
        if (br.hasErrors()) {
            List<ObjectError> allErrors = br.getAllErrors();
            for (ObjectError allError : allErrors) {
                //获取打印提示信息
                System.out.println(allError.getDefaultMessage());
            }
            throw new MyParameterException(500, "参数校验失败");
        }

        orderService.createOrder(form);
        return Result.success(200,"创建订单成功",null);
    }

    @GetMapping("/list")
    @ApiOperation("分页条件查询个人商铺订单信息")
    public Result list() throws Exception {

        List<OrderListDto> dto=orderService.select();
        return Result.success(200,"查询个人订单信息成功",dto);
    }

    @GetMapping("/detail")
    @ApiOperation("查询订单详情")
    public Result viewDetail(String orderNum) throws Exception{
        OrderDetailDto orderDetailDto= orderService.viewDetail(orderNum);
        return Result.success(200,"查询订单详情成功",orderDetailDto);
    }

    @GetMapping("/pay")
    @ApiOperation("支付")
    public Result payOrder(String num) throws Exception{
        System.out.println(num);
        String result= orderService.pay(num);

        return Result.success(200,"调用成功",result);
    }

    @PostMapping("/payback")
    @ApiOperation("支付回调")
    public void payback()throws Exception{
        orderService.payback();
    }


    @PostMapping("/success")
    @ApiOperation("确认订单")
    public Result success(String num) throws Exception{
        orderService.success(num);
        return Result.success(200,"订单已完成",null);
    }



}

